/**
 * @ Description: 推广APP 
 * @ Author: huangmian
 * @ Update: 2014-12-25 10:00
 */
"use strict";


(function(){
	var getLocation = window.location.href;

    var promoteApp = {
    	init:function(){
    		this.getUrlParam('from');
    		
    	},
    	getUrlParam:function(name){
    		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        	var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        	if (r != null){
        		
        		this.render();
        		// return unescape(r[2]);	
        	}else{
        		// alert('没有参数');
        		// return null; //返回参数值
                return;
        	}  
    	},

    	showPromoteTpl:function(){
    		var style_tpl = [
    			'<style>',
    			'#J_beforelogin_tip{display:none!important}',
    			'.bottom-promote{display:none;position: fixed;_position:absolute;bottom:0;left:0;_bottom:auto;_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight));z-index:1000;width:100%;height:100px;font-size:14px;font-family:"Microsoft Yahei";background: rgb(0, 0, 0);background: rgba(0, 0, 0, 0.8);background: transparent\9;*zoom: 1;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)";color:#fff;}',
    			'.bottom-promote-bd{position: relative;width:980px;margin:0 auto;}',
    			'.bp-img{display:block;width:980px;margin:0 auto;height:100px;background:url(http://b3.hoopchina.com.cn/web/widget/partner/2014/hao123/bottom-promote.png) top center no-repeat;}',
    			'*html .bp-img {background:none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://b3.hoopchina.com.cn/web/widget/partner/2014/hao123/bottom-promote.png");}',
                '.bottom-promote .promote-close{position: absolute;top:10px;right:0;font-family:arial;color:#fff;}',
    			'</style>'
    		].join('');

    		var promote_tpl = [
    			'<div class="bottom-promote" id="J_bottom_promote">',
    				'<div class="bottom-promote-bd">',
                    '<div class="bp-img"></div>', 
    					'<a href="javascript:" class="promote-close J_promote_btn_close" title="关闭">X</a>',
    				'</div>',
    			'</div>'
    		].join('');

    		return style_tpl + promote_tpl;
    	},
    	render : function(){
			if ( !$.cookie('kanqiuPromote') ) {
				var _html = this.showPromoteTpl();
				$('body').append(_html);
				this.bind();
			}
			$('.voice-sidebar').append('<a href="http://hao123.lecai.com/lottery/jclq/?agentId=5525" target="_blank"><img src="http://b1.hoopchina.com.cn/web/widget/partner/2014/hao123/cp-link2.jpg" width="300"></a>');
    	},
    	bind:function(){
            var that = this;

            that.$elem = $('#J_bottom_promote');
            that.$btnClose = that.$elem.find(".J_promote_btn_close");

            that.$elem.fadeIn();

            that.$btnClose.bind("click", function(){
                $.cookie('kanqiuPromote', 1, {
                    expires:1,
                    path: '/'
                });
                that.$elem.fadeOut();
            }); 
        }

    };

    $.extend({
        cookie:function(name, value, options){//cookie
            if (typeof value != 'undefined') {
                options = options || {};
                if (value === null) {
                    value = '';
                    options.expires = -1;
                }
                var expires = '';
                if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                    var date;
                    if (typeof options.expires == 'number') {
                        date = new Date();
                        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                    } else {
                        date = options.expires;
                    }
                    expires = '; expires=' + date.toUTCString();
                }
                var path = options.path ? '; path=' + options.path : '';
                var domain = options.domain ? '; domain=' + options.domain : '';
                var secure = options.secure ? '; secure' : '';
                document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
            } else {
                var cookieValue = null;
                if (document.cookie && document.cookie != '') {
                    var cookies = document.cookie.split(';');
                    for (var i = 0; i < cookies.length; i++) {
                        var cookie = $.trim(cookies[i]);
                        if (cookie.substring(0, name.length + 1) == (name + '=')) {
                            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                            break;
                        }
                    }
                }
                return cookieValue;
            }
        }
    });

    $(function(){
            promoteApp.init();
    })
}());